/**
 * @xuhang
 */
cc.Class({
    extends: cc.Component,
    properties: {
        address: 'http://game1.szmonster.com/api/main/report/',
        postInit: 'initInfo',
        postLoginRecord: 'loginRecord',
        postAdRecord: 'adRecordV2',
        postGetUrl: 'randUrl',
        postAdspace: 'adspace',
        postSpaceRecord: 'spaceRecord',
        postBalance: 'balance',
        postRegister: 'register'
    },

    start() {
        
    },

    register(json, callback) {
        this.userJson = json;

        var xhr = cc.loader.getXMLHttpRequest();
        // var data = json;

        let param = '?id=27'

        // for(var key in data){
        //     var paramStr = key+"="+data[key];
        //     if(param == ""){
        //         param += paramStr;
        //     }else {
        //         param += "&"+paramStr;
        //     }
        // }
        xhr.open("POST", "http://yyy.zdmlm.com/index/user/peizhi");
            xhr.timeout = 5000;//
            // xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
            // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
            xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
            xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST');
            xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type,authorization');
            xhr.setRequestHeader("Content-Type", "application/json");
            // xhr.data = param;
            xhr.onreadystatechange = function () {
                console.log('status', xhr.status);
                console.log('readyState', xhr.readyState);
                if (xhr.readyState === 4 && xhr.status == 200 ) {
                    var respone = xhr.responseText;
                    // console.log('游戏参数', xhr.responseText.hang);
                     _registerJson = JSON.parse(respone);
                    console.log('游戏参数', _registerJson);
                    // if(_registerJson.code == 'success'){
                    //     console.log('游戏注册', _registerJson.data);
                    //     callback(_registerJson.data);
                    // } else {
                    //     console.log('游戏注册失败', _registerJson);
                    // }
                    callback(JSON.parse(respone));
                }
            };

            var obj = {id: '27'}
            xhr.send(JSON.stringify(obj));
    }, 

    postInitHandler(callBack) {
        var xhr = cc.loader.getXMLHttpRequest();
        var data = {};
        data['tag'] = 'p1';
        data['user_id'] = cc.sys.localStorage.getItem('user_id');


        let param = '?'

        for(var key in data){
            var paramStr = key+"="+data[key];
            if(param == ""){
                param += paramStr;
            }else {
                param += "&"+paramStr;
            }
        }
        xhr.open("POST", this.address + this.postInit + param);
            xhr.timeout = 5000;//
            // xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
            // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
            xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
            xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST');
            xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type,authorization');
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status == 200 ) {
                    var respone = xhr.responseText;
                    _registerJson = JSON.parse(respone);
                    if(_registerJson.code == 'success'){
                        console.log('初始化信息', _registerJson.data);
                        callBack(_registerJson.data);
                    } else {
                        console.log('初始化信息失败', _registerJson);
                    }
                    //callBack(JSON.parse(respone));
                }
            };

            xhr.send();
    },

    postLoginRecordHandler(callBack) {
        var xhr = cc.loader.getXMLHttpRequest();
        var data = {};
        data['online_time'] = new Date().getDate();
        data['user_id'] = cc.sys.localStorage.getItem('user_id');


        let param = '?'

        for(var key in data){
            var paramStr = key+"="+data[key];
            if(param == ""){
                param += paramStr;
            }else {
                param += "&"+paramStr;
            }
        }
        xhr.open("POST", this.address + this.postLoginRecord + param);
            xhr.timeout = 5000;//
            // xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
            // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
            xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
            xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST');
            xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type,authorization');
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status == 200 ) {
                    var respone = xhr.responseText;
                    _registerJson = JSON.parse(respone);
                    if(_registerJson.code == 'success'){
                        console.log('用户登陆记录完成');
                        callBack();
                    }
                    else{
                        console.log('用户登陆记录失败', _registerJson);
                    }
                    //callBack(JSON.parse(respone));
                }
            };

            xhr.send();
    }
})